package club.yuit.desktop.ui.brokers;

import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.JTableHeader;
import java.awt.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static club.yuit.desktop.ui.UIConstants.DEFAULT_SIZE;

/**
 * @author yuit
 * @date 2023/12/10
 **/
public class BrokerKVTableDialog   extends JDialog {

    private Map<String,String> data;
    private JTable table;


    public BrokerKVTableDialog(JFrame parent, String title, Map<String, String> data) {
        this.table = new JTable();
        JTableHeader tableHeader = this.table.getTableHeader();
        DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
        // 设置表头的大小为0
        renderer.setPreferredSize(new Dimension(0,0));
        tableHeader.setDefaultRenderer(renderer);
        tableHeader.setVisible(false);
        this.setSize(DEFAULT_SIZE);

        TableKVModel model = new TableKVModel(data);
        this.table.setModel(model);

        this.data = data;
        this.setResizable(true);
        this.setModal(true);
        this.setLocationRelativeTo(parent);


        this.setTitle(title);

        Container cp = this.getContentPane();
        cp.setLayout(new BorderLayout());

        JPanel panel = new JPanel(){
            @Override
            public Insets getInsets() {
                return new Insets(10,10,10,10);
            }
        };
        panel.setLayout(new BorderLayout());


        panel.add(contentPanel(),BorderLayout.CENTER);
        panel.add(optionsPanel(),BorderLayout.SOUTH);

        cp.add(panel,BorderLayout.CENTER);

    }



    private JPanel contentPanel(){
        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());
        JScrollPane sc = new JScrollPane(table);
        sc.setBorder(null);
        panel.add(sc,BorderLayout.CENTER);
        return panel;
    }

    private JPanel optionsPanel(){
        JPanel panel = new JPanel();
        panel.setLayout(new FlowLayout(FlowLayout.RIGHT));
        JButton ok = new JButton("确定");
        ok.addActionListener(e->{
            this.dispose();
        });
        panel.add(ok);
        return panel;
    }


    public void showDialog(){
       if(!isVisible()){
           this.setVisible(true);
       }
    }



    public static BrokerKVTableDialog create(JFrame parent, String title, Map<String, String> data) {
       return new BrokerKVTableDialog(parent, title, data);
    }

    private static class TableKVModel extends AbstractTableModel {

       private Map<String,String> data;
       private List<Map<String,String>> tableValues = new ArrayList<>();


        public TableKVModel(Map<String, String> data) {
            this.data = data;
            transformData();
        }


        private void transformData(){
            if(data!= null){
                tableValues.clear();
               data.forEach((k,v)->{
                   Map<String,String> map = HashMap.newHashMap(2);
                   map.put("key",k);
                   map.put("value",v);
                   tableValues.add(map);
               });
            }
        }

        @Override
        public int getRowCount() {
            return data.size();
        }

        @Override
        public int getColumnCount() {
            return 2;
        }


        @Override
        public boolean isCellEditable(int rowIndex, int columnIndex) {
            return true;
        }

        @Override
        public Object getValueAt(int rowIndex, int columnIndex) {
            Map<String, String> stringStringMap = tableValues.get(rowIndex);
            return switch (columnIndex) {
                case 0 -> stringStringMap.get("key");
                case 1 -> stringStringMap.get("value");
                default -> null;
            };
        }
    }

}


